package com.dayuanit.dy17.mix.thread;

import java.util.concurrent.*;

public class TestJDKThreadPool {
    public static void main(String[] args) {

            //自定义 拒绝策略
            RejectedExecutionHandler rejectedExecutionHandler = new RejectedExecutionHandler(){
                @Override
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    System.out.println("有个任务被拒绝了");
                    r.run();
                }
            };

            //自定义 线程工厂
            ThreadFactory threadFactory = new ThreadFactory(){
            int i = 0;
            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, "HelloThread" + (i++));
            }
        };

        Runnable task = new Runnable() {
            @Override
            public void run() {
                System.out.println("hello..." + Thread.currentThread().getName());
                try {
                    Thread.sleep(10 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        //创建线程池  一共七个参数 要清楚每个参数的含义
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1,
                3,
                60,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue(2),
                threadFactory,
                new ThreadPoolExecutor.AbortPolicy());

        for (int i=0; i<6; i++) {
            threadPoolExecutor.execute(task);
        }
    }
}
